package com.share.example.selenium.service.amazon.seller;

import com.share.common.utils.DelayUtil;
import com.share.example.selenium.service.AbsDriver;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Data
@Slf4j
@Service
public class AmazonSellerLoginService extends AbsDriver {
    private final String LOGIN_URL_PRE = "https://sellercentral.amazon.com/ap/signin";
    private final String LOGIN_SUCCESS_FORWORD_URL_PRE = "https://sellercentral.amazon.com/ap/signin";

    @Autowired
    protected WebDriver driver;

    @Override
    public void reInitDriver(WebDriver driver) {
        this.driver = driver;
    }

    //1.打开卖家---填写表单页面
    public Boolean openLoginPage() {
        //1.打开门户网站首页
        String url = "https://sellercentral.amazon.com/";
        driver.get(url);

        //2.点击登录按钮---跳转至登录页面
        WebElement element = driver.findElement(By.xpath("/html/body/div/div[1]/div/div/div[1]/div[2]/div/div[2]/div[1]/div/a/strong"));
        element.click();

        String currentUrl = driver.getCurrentUrl();
        if (currentUrl.contains(LOGIN_URL_PRE)) {
            return true;
        } else {
            log.info("打开登录表单页面失败,currentUrl={}", currentUrl);
            return false;
        }
    }

    //填写卖家表单页面
    public Boolean fixLoginform(String account, String password) {
        String currentUrl = driver.getCurrentUrl();
        if (!currentUrl.contains(LOGIN_URL_PRE)) {
            log.info("当前不是登录表单的页面,currentUrl={}", currentUrl);
            return false;
        }

        //1.填写账号
        if (StringUtils.isEmpty(account)) {
            account = "111123";
        }
        WebElement ele_account = driver.findElement(By.id("ap_email"));
        ele_account.sendKeys(account);
        DelayUtil.sleepRangSecond(1, 4);

        //2.填写密码
        if (StringUtils.isEmpty(password)) {
            password = "111123";
        }
        WebElement ele_password = driver.findElement(By.id("ap_password"));
        ele_password.sendKeys(password);
        DelayUtil.sleepRangSecond(1, 4);

        //3.勾选复选框
        WebElement check_box = driver.findElement(By.xpath("//*[@id=\"authportal-main-section\"]/div[2]/div/div/form/div/div/div/div[3]/div[2]/div/label/div/label/input"));
        check_box.click();
        DelayUtil.sleepRangSecond(1, 4);

        //5.点击登录
        WebElement loginButton = driver.findElement(By.id("a-autoid-0-announce"));
        loginButton.submit();

        DelayUtil.sleepRangSecond(3, 10);

        String forward_currentUrl = driver.getCurrentUrl();
        if (forward_currentUrl.contains(LOGIN_SUCCESS_FORWORD_URL_PRE)) {
            return true;
        } else {
            log.info("登录失败,forward_currentUrl={}", forward_currentUrl);
            return false;
        }
    }
}
